lionsApp
    .controller('identityCtrl', ['$scope', '$state', '$rootScope', 'lionsHttp', function ($scope, $state, $rootScope, lionsHttp) {
        lionsHttp.areaList({}, {}, function (data) {
            $scope.areaList = data.data
        })
        $scope.areaChange = function () {
            $scope.userName = ''
            if($scope.userArea == 999){
                $scope.noName = true
                $scope.areaName = '爱心人士'
                $scope.teamName = '爱心人士'
            }else{
                $scope.noName = false
                $scope.areaList.forEach( function (item) {
                    if(item.area == $scope.userArea){
                        $scope.areaName = item.areaName
                    }
                })
            }
            lionsHttp.teamList({}, {area: $scope.userArea}, function (data) {
                $scope.teamList = data.data
            })
        }
        $scope.teamChange = function () {
            $scope.teamList && $scope.teamList.forEach( function (item) {
                if(item.id == $scope.userTeam){
                    $scope.teamName = item.name
                }
            })
            lionsHttp.userList({}, {team: $scope.userTeam}, function (data) {
                $scope.userList = data.data
            })
        }
        $scope.userChange = function () {
            $scope.userList && $scope.userList.forEach( function (item) {
                if(item.id == $scope.userId){
                    $scope.userName = item.name
                    $scope.error = false
                }
            })
        }
        $scope.identity = function () {
            if(!$scope.userName){
                $scope.error = '请填写您的姓名'
                return
            }
            $scope.goConfirm = true
            $scope.confirmError = '一旦认领，该身份就属于您，不可修改！'
            $scope.errorBind = false
        }
        $scope.confirmIdentity = function () {
            $scope.confirmError = '提交中，请等待...'
            if(!$scope.btnDisabled){
                $scope.btnDisabled = true
                lionsHttp.addBind({}, {
                    userId: $scope.userId,
                    mobile: $scope.mobile,
                    userName: $scope.userName
                }, function (data) {
                    $scope.btnDisabled = false
                    if(data.statusCode == 0){
                        $state.go('home')
                    }else if(data.statusMessage == '用户信息不存在'){
                        $scope.confirmError = false
                        $scope.errorBind = true
                    }else{
                        $scope.confirmError = data.statusMessage
                    }
                })
            }
        }
        $scope.reSelect = function () {
            $scope.goConfirm = false
        }
    }])